<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="jsonp," />










<meta name="description" content="前端跨域方案有很多种：1、img标签实现跨域：1&amp;lt;img src=&quot;http://zgo.tech?name=zgo&amp;amp;&quot; alt=&quot;&quot;&amp;gt; 2、link标签实现跨域：1&amp;lt;link rel=&quot;stylesheet&quot; href=&quot;http://zgo.tech&quot;&amp;gt; 3、iframe标签实现跨域：1&amp;lt;iframe src=&quot;&quot; frameborder=&quot;0&quot;&amp;gt;&amp;">
<meta name="keywords" content="jsonp">
<meta property="og:type" content="article">
<meta property="og:title" content="JSONP">
<meta property="og:url" content="http://yoursite.com/2018/05/26/jsonp/index.html">
<meta property="og:site_name" content="郭志强的博客">
<meta property="og:description" content="前端跨域方案有很多种：1、img标签实现跨域：1&amp;lt;img src=&quot;http://zgo.tech?name=zgo&amp;amp;&quot; alt=&quot;&quot;&amp;gt; 2、link标签实现跨域：1&amp;lt;link rel=&quot;stylesheet&quot; href=&quot;http://zgo.tech&quot;&amp;gt; 3、iframe标签实现跨域：1&amp;lt;iframe src=&quot;&quot; frameborder=&quot;0&quot;&amp;gt;&amp;">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2018-05-27T09:17:41.589Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="JSONP">
<meta name="twitter:description" content="前端跨域方案有很多种：1、img标签实现跨域：1&amp;lt;img src=&quot;http://zgo.tech?name=zgo&amp;amp;&quot; alt=&quot;&quot;&amp;gt; 2、link标签实现跨域：1&amp;lt;link rel=&quot;stylesheet&quot; href=&quot;http://zgo.tech&quot;&amp;gt; 3、iframe标签实现跨域：1&amp;lt;iframe src=&quot;&quot; frameborder=&quot;0&quot;&amp;gt;&amp;">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2018/05/26/jsonp/"/>





  <title>JSONP | 郭志强的博客</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">郭志强的博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <h1 class="site-subtitle" itemprop="description">技术路的心得体会</h1>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/05/26/jsonp/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Zgo">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="https://wx3.sinaimg.cn/mw690/006y2FJQgy1fqsfkkm9jsj30qn0qngog.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="郭志强的博客">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">JSONP</h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-05-26T18:38:36+08:00">
                2018-05-26
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/前端/" itemprop="url" rel="index">
                    <span itemprop="name">前端</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="前端跨域方案有很多种："><a href="#前端跨域方案有很多种：" class="headerlink" title="前端跨域方案有很多种："></a>前端跨域方案有很多种：</h1><h2 id="1、img标签实现跨域："><a href="#1、img标签实现跨域：" class="headerlink" title="1、img标签实现跨域："></a>1、img标签实现跨域：</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;img src=<span class="string">"http://zgo.tech?name=zgo&amp;"</span> alt=<span class="string">""</span>&gt;</span><br></pre></td></tr></table></figure>
<h2 id="2、link标签实现跨域："><a href="#2、link标签实现跨域：" class="headerlink" title="2、link标签实现跨域："></a>2、link标签实现跨域：</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;link rel=<span class="string">"stylesheet"</span> href=<span class="string">"http://zgo.tech"</span>&gt;</span><br></pre></td></tr></table></figure>
<h2 id="3、iframe标签实现跨域："><a href="#3、iframe标签实现跨域：" class="headerlink" title="3、iframe标签实现跨域："></a>3、iframe标签实现跨域：</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;iframe src=<span class="string">""</span> frameborder=<span class="string">"0"</span>&gt;&lt;/iframe&gt;</span><br></pre></td></tr></table></figure>
<h2 id="4、script标签实现跨域"><a href="#4、script标签实现跨域" class="headerlink" title="4、script标签实现跨域"></a>4、script标签实现跨域</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;script src=<span class="string">"http://zgo.tech"</span>&gt;&lt;/script&gt;</span><br></pre></td></tr></table></figure>
<p>以上几种方法都能实现跨域，img、link、script本质是相同的，向服务器发送请求，得到外部服务器响应，外部服务器既然能响应，说明这三种同样都能给服务器传递数据，通过把数据放到rul?后，所以就发送数据来讲，三者都是可以用的；iframe是通过往当前网页中内嵌外部网页实现的跨域请求。其中img、link获取的数据，却只能被当前固定格式识别，即在其他部分数据是不可读的。iframe通过内嵌，可以在本网页获取数据但是却不利于结构、SEO等。而script向网页中插入js代码：<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">func(data);</span><br></pre></td></tr></table></figure></p>
<p>其中数据的获取要根据需求，向script.src地址中传入参数，供外部服务器接收使用。回调函数，用传入的callback同名函数，接收外部服务器返回的data，供本网页使用。这就是jsonp。</p>
<p>jsonp对于网页加载各方面有很大优势，通过跨域，把数据放在不同服务器，其实相当与分布式处理，对于中心处理服务器来说，减轻压力，增加了数据的并行，对网页渲染速度有益。这种优势对大公司大业务来说会更加明显，分散服务器压力，对于正常访问还是非正常访问都是有好处的。</p>
<p>但是对于小型业务来说，ajax更简单易用，通过自用服务器接收传入参数，并包装数据，给前端ajax调用，使用方式能更加灵活简单。</p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/jsonp/" rel="tag"># jsonp</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/05/23/90后/" rel="next" title="90后">
                <i class="fa fa-chevron-left"></i> 90后
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="https://wx3.sinaimg.cn/mw690/006y2FJQgy1fqsfkkm9jsj30qn0qngog.jpg"
                alt="Zgo" />
            
              <p class="site-author-name" itemprop="name">Zgo</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">6</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">2</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">3</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/zgoby" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://weibo.com/u/5999829518?refer_flag=1001030101_&is_all=1" target="_blank" title="Weibo">
                      
                        <i class="fa fa-fw fa-weibo"></i>Weibo</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#前端跨域方案有很多种："><span class="nav-number">1.</span> <span class="nav-text">前端跨域方案有很多种：</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1、img标签实现跨域："><span class="nav-number">1.1.</span> <span class="nav-text">1、img标签实现跨域：</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2、link标签实现跨域："><span class="nav-number">1.2.</span> <span class="nav-text">2、link标签实现跨域：</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3、iframe标签实现跨域："><span class="nav-number">1.3.</span> <span class="nav-text">3、iframe标签实现跨域：</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4、script标签实现跨域"><span class="nav-number">1.4.</span> <span class="nav-text">4、script标签实现跨域</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Zgo</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
